package com.uniview.web.contorller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.uniview.base.BaseController;
import com.uniview.common.mapper.JsonMapper;
import com.uniview.entity.po.Employees;
import com.uniview.entity.po.RegionManager;
import com.uniview.entity.po.ServiceOrders;
import com.uniview.entity.po.ServiceOrdersLogs;
import com.uniview.entity.vo.OrderMessage;
import com.uniview.entity.vo.Pager;
import com.uniview.entity.vo.ResultMessage;
import com.uniview.entity.vo.TableJsonVO;
import com.uniview.plugins.workplus.WorkplusMessagePushManager;
import com.uniview.service.EmployeesService;
import com.uniview.service.RegionManagerService;
import com.uniview.service.ServiceOrdersLogsService;
import com.uniview.service.ServiceOrdersService;
import com.uniview.web.api.ServiceOrderApi;

@Controller
@RequestMapping(value = "/order")
public class ServiceOrdersController extends BaseController {

	Logger logger = Logger.getLogger(ServiceOrdersController.class);

	@Autowired
	private ServiceOrdersService srviceOrdersService;

	@Autowired
	private ServiceOrdersLogsService serviceOrdersLogsService;

	// @Autowired
	// private ExternalTestService externalTestService; // 对外测试数据接口

	// @Autowired
	// private OnlineProblemsService onlineProblemsService; // 在线问题数据接口

	// @Autowired
	// private ProjectDeliveryService projectDeliveryService; // 项目交付数据接口

	// @Autowired
	// private RmaService rmaService; // 备件维修数据接口

	@Autowired
	private WorkplusMessagePushManager pushManager;

	@Autowired
	private EmployeesService employeesService;

	@Autowired
	private RegionManagerService managerService;

	@Autowired
	private ServiceOrdersLogsService ordersLogsService;

	@Autowired
	private ServiceOrderApi serviceOrderApi;

	@Value("#{configProperties['uniview.deptLevel']}")
	private int level;

	@Value("#{configProperties['uniview.superAdmin']}")
	private String superAdmin;

	@InitBinder
	protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	}

	/**
	 * 获取对外测试列表数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findExternalTestByworkNo")
	@ResponseBody
	public String findExternalTestByworkNo(HttpServletRequest request) {
		return serviceOrderApi.findExternalTestByworkNo(request);
		/*
		 * try { int page = ServletRequestUtils.getIntParameter(request, "page", 0); int
		 * limit = ServletRequestUtils.getIntParameter(request, "limit", 10); String
		 * workNo = ServletRequestUtils.getStringParameter(request, "workNo"); String
		 * queryString = ServletRequestUtils.getStringParameter(request, "queryString");
		 * Pager<ExternalTest> pager =
		 * externalTestService.findExternalTestByWorkNo(workNo, queryString, page,
		 * limit); if (pager != null && pager.getPagerList() != null &&
		 * pager.getPagerList().size() > 0) { return
		 * getTableJsonString(JsonMapper.toJsonString(pager.getPagerList()),
		 * pager.getTotalRow()); } } catch (Exception e) { logger.error("获取对外测试列表数据异常",
		 * e); } return getTableJsonString("[]", 0);
		 */
	}

	/**
	 * 获取网上问题列表数据
	 */
	@RequestMapping(value = "/findOnlieProblemsByworkNo")
	@ResponseBody
	public String findOnlieProblemsByworkNo(HttpServletRequest request) {
		return serviceOrderApi.findOnlieProblemsByworkNo(request);
		/*
		 * try { int page = ServletRequestUtils.getIntParameter(request, "page", 0); int
		 * limit = ServletRequestUtils.getIntParameter(request, "limit", 10); String
		 * workNo = ServletRequestUtils.getStringParameter(request, "workNo"); String
		 * queryString = ServletRequestUtils.getStringParameter(request, "queryString");
		 * Pager<OnlineProblems> pager =
		 * onlineProblemsService.findOnlineProblemsByWorkNo(workNo, queryString, page,
		 * limit); if (pager != null && pager.getPagerList() != null &&
		 * pager.getPagerList().size() > 0) { return
		 * getTableJsonString(JsonMapper.toJsonString(pager.getPagerList()),
		 * pager.getTotalRow()); } } catch (ServletRequestBindingException e) {
		 * logger.error("获取网上问题列表数据异常", e); } catch (Exception e) {
		 * logger.error("获取网上问题列表数据异常", e); } return getTableJsonString("[]", 0);
		 */
	}

	/**
	 * 获取项目交付列表数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findProjectDeliveryByworkNo")
	@ResponseBody
	public String findProjectDeliveryByworkNo(HttpServletRequest request) {
		return serviceOrderApi.findProjectDeliveryByworkNo(request);
		/*
		 * try { int page = ServletRequestUtils.getIntParameter(request, "page", 0); int
		 * limit = ServletRequestUtils.getIntParameter(request, "limit", 10); String
		 * workNo = ServletRequestUtils.getStringParameter(request, "workNo"); String
		 * queryString = ServletRequestUtils.getStringParameter(request, "queryString");
		 * if (StringUtils.isNotBlank(workNo)) { workNo = workNo.toUpperCase(); }
		 * Pager<ProjectDelivery> pager =
		 * projectDeliveryService.findProjectDeliveryByWorkNo(workNo, queryString, page,
		 * limit); if (pager != null && pager.getPagerList() != null &&
		 * pager.getPagerList().size() > 0) { return
		 * getTableJsonString(JsonMapper.toJsonString(pager.getPagerList()),
		 * pager.getTotalRow()); } } catch (Exception e) { logger.error("获取项目交付列表数据异常",
		 * e); } return getTableJsonString("[]", 0);
		 */
	}

	/**
	 * 获取备件维修 列表数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findRmaByworkNo")
	@ResponseBody
	public String findRmaByworkNo(HttpServletRequest request) {
		return serviceOrderApi.findRmaByworkNo(request);
		/*
		 * try { int page = ServletRequestUtils.getIntParameter(request, "page", 0); int
		 * limit = ServletRequestUtils.getIntParameter(request, "limit", 10); String
		 * workNo = ServletRequestUtils.getStringParameter(request, "workNo"); String
		 * queryString = ServletRequestUtils.getStringParameter(request, "queryString");
		 * Pager<Rma> pager = rmaService.findRmaByWorkNo(workNo, queryString, page,
		 * limit); if (pager != null && pager.getPagerList() != null &&
		 * pager.getPagerList().size() > 0) { return
		 * getTableJsonString(JsonMapper.toJsonString(pager.getPagerList()),
		 * pager.getTotalRow()); } } catch (Exception e) { logger.error("获取备件维修列表数据异常",
		 * e); } return getTableJsonString("[]", 0);
		 */
	}

	/**
	 * 工单处理列表
	 * 
	 * @param request
	 * @param empCode
	 * @return
	 */
	@RequestMapping(value = "/findServiceOrders")
	@ResponseBody
	public String findServiceOrders(HttpServletRequest request, String empCode) {
		try {
			int page = ServletRequestUtils.getIntParameter(request, "page", 0);
			int limit = ServletRequestUtils.getIntParameter(request, "limit", 10);
			// 当时用户
			String creator = ServletRequestUtils.getStringParameter(request, "creator");
			String handler = ServletRequestUtils.getStringParameter(request, "handler");
			String startDate = ServletRequestUtils.getStringParameter(request, "startDate");
			String endDate = ServletRequestUtils.getStringParameter(request, "endDate");
			String area = ServletRequestUtils.getStringParameter(request, "area");
			Integer status = ServletRequestUtils.getIntParameter(request, "status", -1);
			// 是否为经理
			String empName = "";
			String region = "";
			RegionManager manager = managerService.findRegionManagerByWorkNo(empCode);
			Employees employees = employeesService.findEmployeesByWorkNo(empCode);
			empName = employees.getEmpName();
			if (null != manager) {
				region = employees.feachOffice(level);
			}
			Pager<ServiceOrders> pager = srviceOrdersService.findAllOrders(creator, handler, empName, region, startDate,
					endDate, area, status, limit, page);
			if (pager != null && pager.getPagerList() != null && pager.getPagerList().size() > 0) {
				return getTableJsonString(JsonMapper.toJsonString(pager.getPagerList()), pager.getTotalRow());
			}
		} catch (Exception e) {
			logger.error("获取工单列表数据异常", e);
		}
		return getTableJsonString("[]", 0);
	}

	@RequestMapping(value = "/findAllOrdersByWeb")
	@ResponseBody
	public TableJsonVO findAllOrdersByWeb(HttpServletRequest request, String empCode) {
		try {
			int pageNo = ServletRequestUtils.getIntParameter(request, "page", 0);
			int pageSize = ServletRequestUtils.getIntParameter(request, "limit", 10);
			String creator = ServletRequestUtils.getStringParameter(request, "creator");
			String handler = ServletRequestUtils.getStringParameter(request, "handler");
			String startDate = ServletRequestUtils.getStringParameter(request, "startDate");
			String endDate = ServletRequestUtils.getStringParameter(request, "endDate");
			Integer status = ServletRequestUtils.getIntParameter(request, "status", -1);
			String orderNo = ServletRequestUtils.getStringParameter(request, "searchOrderNo");

			Pager<ServiceOrders> pager = null;
			logger.debug("======== superAdmin:" + this.superAdmin + ",empcode:" + empCode);
			if (this.superAdmin.equalsIgnoreCase(empCode)) {
				// 管理员获取所有工单
				pager = srviceOrdersService.findAllOrdersByAdmin(creator, handler, startDate, endDate, status, orderNo,
						pageSize, pageNo);
			} else {
				RegionManager rm = managerService.findRegionManagerByWorkNo(empCode);

				if (null != rm) {
					// 服务经理，获取所在办事情下属员工的工单，对应状态
					pager = srviceOrdersService.findAllOrdersByWeb(2, empCode, creator, handler, startDate, endDate,
							status, orderNo, pageSize, pageNo);
				} else {
					// 工程师或员工，自己管理的工单，对应状态
					pager = srviceOrdersService.findAllOrdersByWeb(1, empCode, creator, handler, startDate, endDate,
							status, orderNo, pageSize, pageNo);
				}
			}
			if (pager != null && pager.getPagerList() != null && pager.getPagerList().size() > 0) {
				// return getTableJsonString(JsonMapper.toJsonString(pager.getPagerList()),
				// pager.getTotalRow());
				return getTableJsonVO(pager.getPagerList(), pager.getTotalRow());
			}
		} catch (Exception e) {
			logger.error("获取工单列表数据异常", e);
		}

		// return getTableJsonString("[]", 0);
		return getTableJsonVO(new ArrayList<ServiceOrders>(), 0);
	}

	/**
	 * 新增工单
	 * 
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "/addOrder")
	@ResponseBody
	public ResultMessage addOrder(ServiceOrders order, @RequestParam(value = "ccList[]") String[] ccList) {
		return serviceOrderApi.addOrder(order, ccList);
	}

	@RequestMapping(value = "/deleteOrder")
	@ResponseBody
	public ResultMessage deleteOrder(int orderId) {
		String msg = "删除失败";
		boolean result = false;
		try {
			result = srviceOrdersService.deleteOrder(orderId);
			if (result) {
				msg = "删除成功！";
			}
		} catch (Exception e) {
			msg = "删除工单异常！";
			logger.error(msg, e);
		}
		return getResult(msg, result);
	}

	@RequestMapping(value = "/updateOrder")
	@ResponseBody
	public ResultMessage updateOrder(ServiceOrders order) {
		String msg = "修改失败";
		boolean result = false;
		try {
			order.setStatus("0");
			order.setManagerNo(order.getHandleNo());
			ServiceOrdersLogs logs = new ServiceOrdersLogs();
			logs.setCreateTime(new Date());
			logs.setCreator(order.getCreator());
			logs.setCreatorNo(order.getCreatorNo());
			logs.setHandler("修改工单");
			logs.setStauts(0);
			logs.setOrderId(order.getId());
			logs.setReMark("工单被驳回，创建人修改工单");
			result = srviceOrdersService.updateOrder(order, logs);
			if (result) {
				msg = "修改成功！";
			}
		} catch (Exception e) {
			msg = "修改工单异常！";
			logger.error(msg, e);
		}
		return getResult(msg, result);
	}

	@RequestMapping(value = "/applyOrder")
	@ResponseBody
	public ResultMessage applyOrder(HttpServletRequest request) {
		String msg = "审核失败";
		boolean result = false;
		try {
			int isUrgent = ServletRequestUtils.getIntParameter(request, "isUrgent", 0);
			int orderId = ServletRequestUtils.getIntParameter(request, "orderId", -1);
			String applyDesc = ServletRequestUtils.getStringParameter(request, "applyDesc", "审批同意");
			/*
			 * String handler = ServletRequestUtils.getStringParameter(request, "handler");
			 * String handlerNo = ServletRequestUtils.getStringParameter(request,
			 * "handlerNo"); String handlerDept =
			 * ServletRequestUtils.getStringParameter(request, "handlerDept");
			 */
			if (orderId > 0) {
				ServiceOrders order = srviceOrdersService.featchOrders(orderId);
				if (order != null) {
					ServiceOrdersLogs logs = new ServiceOrdersLogs();
					order.setModifyTime(new Date());

					logs.setCreator(order.getHandle());
					logs.setCreatorNo(order.getHandleNo());
					logs.setCreateTime(new Date());
					logs.setHandler(applyDesc);
					if (isUrgent == 0) { // 工程师同意
						order.setStatus("1"); // 审核通过
						/* logs.setReMark("同意"); */
						logs.setStauts(1);
					} else { // 工程师拒绝
						// 回到工单指派的服务经理上
						RegionManager manager = managerService.findRegionManagerByWorkNo(order.getManagerNo());
						if (null != manager) {
							order.setHandle(manager.getManager());
							order.setHandleNo(manager.getWorkNo().toUpperCase());
							order.setHandlerDept(manager.getRegionName());

							// 流转到服务经理
							logs.setAssignName(manager.getManager());
						}
						/**
						 * ServiceOrdersLogs ol = ordersLogsService.findApplyLogsByCreator(orderId); if
						 * (ol != null) { 更改处理人，退回到服务经理 Employees emp =
						 * employeesService.findEmployeesByWorkNo(ol.getCreatorNo()); if (emp != null) {
						 * // 办事处名称 RegionManager manager =
						 * managerService.findRegionManagerByWorkNo(emp.getEmpCode()); if (null !=
						 * manager) { order.setHandle(manager.getManager());
						 * order.setHandleNo(manager.getWorkNo().toUpperCase());
						 * order.setHandlerDept(manager.getRegionName());
						 * 
						 * // 流转到服务经理 logs.setAssignName(manager.getManager()); } } }
						 */

						order.setStatus("0"); //
						logs.setReMark("不接受，请重新指定工程师");
						logs.setStauts(0);
					}

					result = srviceOrdersService.approveOrders(order, logs);

					if (result) {
						msg = "审核成功";
						// 发送推送消息
						String targetAlert = "";
						String handleType = "";
						if (order.getStatus().equals("1")) {
							targetAlert = "服务派工[工单审核]通知";
							handleType = "审核";
						} else {
							targetAlert = "服务派工[工单驳回]通知";
							handleType = "驳回";
						}

						OrderMessage orderMessage = serviceOrderApi.getOrderMessage(handleType,
								serviceOrderApi.serviceOrderUrl, order);
						List<String> userNames = new ArrayList<String>();
						userNames.add(order.getHandleNo());
						this.pushManager.pushMessage(targetAlert, "", targetAlert, orderMessage, userNames);

					}
				}
			}

		} catch (Exception e) {
			msg = "审核工单异常";
			logger.error(msg, e);
		}

		return getResult(msg, result);
	}

	@RequestMapping(value = "/applyOrderByManager")
	@ResponseBody
	public ResultMessage applyOrderByManager(HttpServletRequest request) {
		String msg = "审核失败";
		boolean result = false;
		try {
			int isUrgent = ServletRequestUtils.getIntParameter(request, "isUrgent", 0);
			int orderId = ServletRequestUtils.getIntParameter(request, "orderId", -1);
			String applyDesc = ServletRequestUtils.getStringParameter(request, "applyDesc", "审批同意");
			String handler = ServletRequestUtils.getStringParameter(request, "handler");
			String handlerNo = ServletRequestUtils.getStringParameter(request, "handlerNo");
			String handlerDept = ServletRequestUtils.getStringParameter(request, "handlerDept");
			String contactName = ServletRequestUtils.getStringParameter(request, "contactName");
			String customerPhone = ServletRequestUtils.getStringParameter(request, "customerPhone");
			String customerAddress = ServletRequestUtils.getStringParameter(request, "customerAddress");
			if (orderId > 0) {
				ServiceOrders order = srviceOrdersService.featchOrders(orderId);
				// 发送推送消息
				String targetAlert = "";
				String handleType = "";
				if (order != null) {
					order.setContactName(contactName);
					order.setCustomerPhone(customerPhone);
					order.setCustomerAddress(customerAddress);

					ServiceOrdersLogs logs = new ServiceOrdersLogs();
					order.setModifyTime(new Date());
					logs.setCreator(order.getHandle());
					logs.setCreatorNo(order.getHandleNo());
					logs.setCreateTime(new Date());
					logs.setHandler(applyDesc);

					if (isUrgent == 0) { // 服务经理同意
						logs.setReMark("审核处理");
						logs.setStauts(1);
						if (StringUtils.isNotBlank(handlerNo)) {
							if (handlerNo.equalsIgnoreCase(order.getManagerNo())
									|| handlerNo.equalsIgnoreCase(order.getCreatorNo())) {
								// 服务经理指派给自己 或者 指派人是工单创建人
								order.setStatus("1"); // 审核通过
							} else {
								order.setStatus("0"); // 流转给指派人审核
							}
							order.setHandle(handler);
							order.setHandleNo(handlerNo);
							order.setHandlerDept(this.feachOffice(handlerDept, this.level));
						} else {
							order.setStatus("1");
						}
						targetAlert = "服务派工[工单审核]通知";
						handleType = "审核";
					} else { // 拒绝
						if (order.getHandleNo().equalsIgnoreCase(order.getManagerNo())) {
							order.setStatus("3"); // 服务经理驳回，给回创建人修改工单
							// 如果当前处理人是服务经理
							order.setHandle(order.getCreator());
							order.setHandleNo(order.getCreatorNo());
							order.setHandlerDept(this.feachOffice(order.getCreatorDept(), this.level));
							logs.setReMark("服务经理不接受，被驳回，请修改工单");
							logs.setStauts(3);

						} else {
							order.setStatus("0"); // 驳回，回到工单服务经理处，重新指派
							RegionManager rm = this.managerService.findRegionManagerByWorkNo(order.getManagerNo());
							order.setHandle(rm.getManager());
							order.setHandleNo(rm.getWorkNo());
							order.setHandlerDept(rm.getRegionName());
							logs.setReMark("工程师不接受，被驳回，回到服务经理重新指派");
							logs.setStauts(3);
						}
						targetAlert = "服务派工[工单驳回]通知";
						handleType = "驳回";
					}

					result = srviceOrdersService.approveOrders(order, logs);

					if (result) {
						msg = "审核成功";
						OrderMessage orderMessage = serviceOrderApi.getOrderMessage(handleType,
								serviceOrderApi.serviceOrderUrl, order);
						List<String> userNames = new ArrayList<String>();
						userNames.add(order.getHandleNo());
						this.pushManager.pushMessage(targetAlert, "", targetAlert, orderMessage, userNames);

					}
				}
			}

		} catch (Exception e) {
			msg = "审核工单异常";
			logger.error(msg, e);
		}

		return getResult(msg, result);
	}

	/**
	 * 催单
	 */
	@RequestMapping(value = "/urgeOrder")
	@ResponseBody
	public ResultMessage urgeOrder(HttpServletRequest request) {
		return serviceOrderApi.urgeOrder(request);
		/*
		 * String msg ="催单失败"; boolean result = false; try{ int orderId =
		 * ServletRequestUtils.getIntParameter(request, "orderId", -1); if(orderId > 0){
		 * ServiceOrders order = srviceOrdersService.featchOrders(orderId); if(order !=
		 * null){ //获取工单处理人 //String handler = order.getHandle(); //发送推送消息，提醒处理人尽快处理工单
		 * String targetAlert ="服务派工[催办工单]通知"; String handleType = "催单";
		 * SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		 * OrderMessage om = new OrderMessage();
		 * om.setCreateTime(dateFormat.format(order.getCreateTime()));
		 * om.setCustomerAddress(order.getCustomerAddress());
		 * om.setCustomerName(om.getCustomerName());
		 * om.setCustomerPhone(order.getCustomerPhone()); om.setHandleType(handleType);
		 * om.setHandle(order.getHandle()); om.setOrderFrom(order.getOrderFrom());
		 * om.setOrderFromName(order.getOrderFromName());
		 * om.setOrderNo(order.getOrderNo()); om.setUrl("http://www.baidu.com");
		 * List<String> userNames = new ArrayList<String>();
		 * userNames.add(order.getHandleNo()); pushManager.pushMessage(targetAlert, "",
		 * targetAlert, om,userNames);
		 * 
		 * //记录工单操作日志 ServiceOrdersLogs logs = new ServiceOrdersLogs();
		 * logs.setCreateTime(new Date()); logs.setCreator(order.getCreator());
		 * logs.setCreatorNo(order.getCreatorNo()); logs.setHandler("催办工单");
		 * logs.setReMark("催单"); logs.setStauts(1); Integer logsId =
		 * serviceOrdersLogsService.saveOrderLogs(logs); if(logsId > 0 ){ result = true;
		 * msg = "催单成功"; } } } //创建工单操作日志
		 * 
		 * }catch(Exception e){ msg = "催办工单异常"; logger.error(msg, e); }
		 * 
		 * return getResult(msg, result);
		 */
	}

	/**
	 * 查看工单
	 */
	@RequestMapping(value = "orderDetail")
	@ResponseBody
	public ResultMessage getOrderDetail(Integer orderId) {
		try {
			ServiceOrders order = srviceOrdersService.featchOrders(orderId);
			if (null != order) {
				List<ServiceOrdersLogs> list = ordersLogsService.findLogByOrderId(orderId);
				if (null != list && list.size() > 0) {
					order.setLogs(list);
				}
				return getResult("获取成功！", true, order);
			} else {
				return getResult("查无此记录！", false);
			}
		} catch (Exception e) {
			logger.error("获取工单信息失败！", e);
		}
		return getResult("获取失败", false);
	}

	@RequestMapping(value = "/countOrdersByStatusFromDay")
	@ResponseBody
	public Map<String, Object> countOrdersByStatusFromDay(HttpServletRequest request) {
		try {
			HttpSession session = request.getSession();
			Employees employees = (Employees) session.getAttribute("employees");
			int days = ServletRequestUtils.getIntParameter(request, "days", 7);
			String region = employees.feachOffice(level);
			Map<String, Object> ordersCount = null;
			if (employees.getEmpCode().equalsIgnoreCase(this.superAdmin)) {
				ordersCount = srviceOrdersService.countOrdersByStatusFromDayByAdmin(days);
			} else {
				ordersCount = srviceOrdersService.countOrdersByStatusFromDay(employees.getEmpCode().toUpperCase(),
						region, days);
			}
			ordersCount = serviceOrderApi.fillStatus(ordersCount);
			return ordersCount;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("统计订单量异常", e);
		}
		return null;
	}

	@RequestMapping(value = "/checkIsApprovaled")
	@ResponseBody
	public ResultMessage checkIsApprovaled(Integer orderId) {
		try {
			// ServiceOrdersLogs ol =
			// this.serviceOrdersLogsService.findApplyLogsByCreator(orderId);
			ServiceOrdersLogs ol = this.serviceOrdersLogsService.findLatestLogByOrderId(orderId);
			// 是否审核日志
			if (null != ol && ol.getStauts() == 1) {
				// 获取当前处理人的服务经理
				RegionManager rm = this.managerService.findRegionManagerByWorkNo(ol.getCreatorNo());
				if (null != rm) {
					return this.getResult("ok", true);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return this.getResult("fail", false);
	}

	@RequestMapping(value = "/findEmployeesBySearch")
	@ResponseBody
	public TableJsonVO findEmployeesBySearch(String search, String mangerNo, Integer page, Integer limit) {
		ResultMessage rm = serviceOrderApi.findEmployeesBySearch(search, mangerNo, page, limit);
		if (rm.isSuccess()) {
			return this.getTableJsonVO(rm.getList(), rm.getTotal());
		}
		return this.getTableJsonVO(new ArrayList<Employees>(), 0);
	}

	@RequestMapping(value = "/cc/get")
	@ResponseBody
	public ResultMessage getCCByUsername(String username) {
		return this.serviceOrderApi.getCCByUser(username);
	}

	private String feachOffice(String orgName, int level) {
		if (level > 0) {
			if (StringUtils.isNotBlank(orgName)) {
				String[] strs = orgName.split("/");
				if (strs.length >= 5) {
					return strs[4];
				}
			}
		}
		return orgName;
	}
}
